package com.smartstate.dataManagement.quartz;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONArray;
import com.smartstate.dataManagement.dto.EquipmentDTO;
import com.smartstate.dataManagement.entity.Sorting;
import com.smartstate.dataManagement.mapper.EquipmentConfigMapper;
import com.smartstate.dataManagement.mapper.ObjMiddleMapper;
import com.smartstate.dataManagement.mapper.ObjMoudelMapper;
import com.smartstate.dataManagement.mapper.SortingMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
@Component("testJob")
@Log4j2
public class QuartzController {

    @Resource
    private EquipmentConfigMapper equipmentConfigMapper;

    @Resource
    private ObjMiddleMapper objMiddleMapper;

    @Resource
    private ObjMoudelMapper objMoudelMapper;

    @Autowired
    RedisTemplate redisTemplate;


    @Autowired
    SortingMapper sortingMapper;

    /**
     * 分选/工业看板
     *
     * @param param
     */
    public void Sorting(String param) {
        try {
            System.out.println(param);
            List<String> idsByType = findIdsByType(1);
            if (idsByType != null) {
                for (String deviceId : idsByType) {
                    //根据deviceId查询楼层id
                    String idAndType = "";
                    List<String> objId = objMiddleMapper.findByDeviceId(deviceId);
                    if (objId != null) {
                        for (String s : objId) {
                            String type = objMoudelMapper.findIdAndType(s, "1");
                            if (type != null) {
                                idAndType = type;
                                break;
                            }
                        }
                    }
                    //Redis中设备的属性和值
                    Map entries = redisTemplate.opsForHash().entries(deviceId);
                    System.err.println(entries);
                    String tags = findEquipment(deviceId);
                    if (tags != null) {
                        Sorting sorting = new Sorting();
                        sorting.setTs(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                        sorting.setObjId(idAndType);
                        sorting.setDeviceId(deviceId);
                        String s1 = IdUtil.simpleUUID();
                        sorting.setUid(s1);
                        sorting.setId(s1);
                        List<EquipmentDTO.AddressData> addressDataList = JSONArray.parseArray(tags, EquipmentDTO.AddressData.class);
                        for (EquipmentDTO.AddressData addressData : addressDataList) {
                            String attributeName = addressData.getAttributeName();
                            if (attributeName.equals("longParticle")) {
                                sorting.setLongParticle(Float.parseFloat(entries.get(attributeName).toString()));
                            }
                            if (attributeName.equals("littleParticle")) {
                                sorting.setLittleParticle(Float.parseFloat(entries.get(attributeName).toString()));
                            }
                            if (attributeName.equals("dewPoint")) {
                                sorting.setDewPoint(Float.parseFloat(entries.get(attributeName).toString()));
                            }
                            if (attributeName.equals("temperature")) {
                                sorting.setTemperature(Float.parseFloat(entries.get(attributeName).toString()));
                            }
                            if (attributeName.equals("humidity")) {
                                sorting.setHumidity(Float.parseFloat(entries.get(attributeName).toString()));
                            }
                            if (attributeName.equals("staticPressure")) {
                                sorting.setStaticPressure(Float.parseFloat(entries.get(attributeName).toString()));
                            }
                        }
                        boolean b = sortingMapper.addSorting(sorting);
                        System.err.println("插入是否成功:" + true);
                    }
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            log.info("工业看板数据存入TD失败");
        }
    }


    public List<String> findIdsByType(Integer deviceType) {
        return equipmentConfigMapper.findIdsByType(deviceType);

    }

    private String findEquipment(String deviceId) {
        return equipmentConfigMapper.selectTages(deviceId);
    }

}
